

import Vue from "vue"
import Vuex from "vuex"
import { getBanner, login ,getTopic} from "../api"
import router from "../router"
import { Message } from "element-ui"
// import request from "request"
import persistedstate from 'vuex-persistedstate'
// import {account} from "../api/mine"
Vue.use(Vuex)

//store/index.js```
//为了避免浏览器兼容性不同报错，使用try

export default new Vuex.Store({
  state: {
    // productId:'',
    // topiclist:[],
    name:"",
    drawer:false,
    userimg:"",
    userid: '',
    username:"",
    token:'', 
    cookie: "",
    banner: [],
    uid: '',
    index: 0,
    imgsrc: 'http://192.168.1.19:8080/',
  },
  mutations: {
    // SET_productId(state, productId) {
    //   state.productId = productId
    // },
    SET_NAME(state,name) {
      state.name = name
    },
    SET_DRAWER(state,drawer) {
      state.drawer = drawer
    },
    SET_TOPLIST(state, topiclist) {
      state.topiclist = topiclist
    },
    SET_IMG(state, userimg) {
      state.userimg =state.imgsrc+ userimg
    },
    // SET_USERID(state,userid) {
    //   state.userid = userid
    // },
    SET_USERNAME(state,username) {
      state.username = username
    },
    SET_TOKEN(state,token) {
      state.token = token
    },
    SET_COOKIE(state, cookie) {
      state.cookie = cookie
    },
    SET_BANNER(state, banner) {
      state.banner = banner
    },
    SET_USERID(state, uid) {
      state.uid = uid
    },
    setIndex(state, index) {
      state.index = index
    },
    setCity(state,city){
      state.city = city
    },
    setProvince(state,province){
      state.province = province
    },
		setAddress(state,addressList){
		 state.addressList = addressList
		},
	
	
  },

  actions: {
    getTopic(context, user) {
      getTopic( user).then(res => {
        // console.log(1);
        console.log(res, 'sssiiio');
        context.commit('SET_TOPLIST',res.data.data)
      })
    },
    login(context, user) {
      // console.log(user,"userInfo");
      login(user).then(res => {
        console.log(res, "userInfo");

        if (res.data.code === 10000) {
          context.commit('SET_TOKEN',res.data.msg)
          console.log(res.data.msg);
          context.commit('SET_USERNAME', res.data.data.nickname)
          context.commit('SET_USERID', res.data.data.userId)
          context.commit('SET_IMG',res.data.data.userImg)
          // context.commit('SET_COOKIE',res.data.cookie)
          // context.commit('SET_USERID',res.data.account.id)
          Message({
            message: "登录成功",
            type: "success",
            duration: 2500
          })
          setTimeout(() => {
            router.push('/')
          }, 2000)
        } else {
          Message({
            message: "用户名或密码错误",
            type: "error",
            duration: 2500
          })
        }
      })
    },
    getBanner(context) {
      getBanner().then(res => {
        console.log(res, "adasdasdasd")
        context.commit("SET_BANNER", res.data.banners)
      })
    }
  },

  getters:  {
    doubleCity  (state)  {
      return state.city + ' ' + state.city //返回一个两个city值加上一个空格的字符串
    }
  },
  // plugins:[
  //   persistedState({})
  // ],
  modules: {
  },
  plugins: [persistedstate({})]
})
// export default store

